package com.tongtong.middleware.blockchain.client.bsn;

import cn.hutool.json.JSONUtil;
import com.tongtong.middleware.vo.params.bsn.*;
import com.tongtong.middleware.vo.response.bsn.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @author wangqi
 */
@Service
@Slf4j
public class BSNService {
	@Autowired
	BSNConfig config;

	/**
	 * 开放联盟链框架查询
     */
	public FrameworkSearchesRes getFrameworkSearches() {
		
		FrameworkSearchesRes res = new FrameworkSearchesRes();
		
		try {
			String apiToken = config.getApiToken();
			String msg = config.conn().frameworkSearches(apiToken);
			res = JSONUtil.toBean(msg, FrameworkSearchesRes.class);
		} catch (Exception e) {
			log.error("发生异常", e);
		}
		
		return res;
	}
	/**
	 * 我的算力余额查询
	 */
	public PowerBalanceRes getPowerBalance() {

		PowerBalanceRes res = new PowerBalanceRes();

		try {
			String apiToken = config.getApiToken();
			String msg = config.conn().powerBalance(apiToken);
			res = JSONUtil.toBean(msg, PowerBalanceRes.class);
		} catch (Exception e) {
			log.error("发生异常", e);
		}

		return res;
	}
	/**
	 * 能量值充值
	 */
	public RechargeRes recharge(RechargeRequest req) {

		RechargeRes res = new RechargeRes();

		try {
			String apiToken = config.getApiToken();
			String msg = config.conn().recharge(apiToken, JSONUtil.toJsonStr(req));
			res = JSONUtil.toBean(msg, RechargeRes.class);
			log.info("recharge: address=>{} value=>{}", res.getData().getOpbChainClientAddress(), res.getData().getEnergyValue());
		} catch (Exception e) {
			log.error("发生异常", e);
		}

		return res;
	}
	/**
	 * 导入链账户
	 */
	@Async
	public void importAccount(ImportAccountRequest req) {
		Object res;

		try {
			String apiToken = config.getApiToken();
			String msg = config.conn().importAccount(apiToken, JSONUtil.toJsonStr(req));
			res = JSONUtil.toBean(msg, Object.class);
            log.info("importAccount:response=>{}", res);
		} catch (Exception e) {
			log.error("发生异常", e);
		}
	}
	/**
	 * NFT列表查询
	 * Token状态 1=正常 4=销毁中 5=销毁 20=发送中 25=发送失败
	 */
	public Object getNFTData(NFTDataRequest req) {

		Object res = new Object();

		try {
			String apiToken = config.getApiToken();
			String msg = config.conn().nftData(apiToken, JSONUtil.toJsonStr(req));
			res = JSONUtil.toBean(msg, Object.class);
		} catch (Exception e) {
			log.error("发生异常", e);
		}

		return res;
	}
	/**
	 * 查看NFT详情
	 */
	public Object getNftDetails(NFTDetailsRequest req) {

		Object res = new Object();

		try {
			String apiToken = config.getApiToken();
			String msg = config.conn().nftDetails(apiToken, JSONUtil.toJsonStr(req));
			res = JSONUtil.toBean(msg, Object.class);
		} catch (Exception e) {
			log.error("发生异常", e);
		}

		return res;
	}
	/**
	 * 根据流水号查询对应NFT交易详情
	 * 交易类型 40=NFT生成 42=NFT流转 43=NFT销毁
	 */
	public NFTTransferListRes getNftTransferInfo(NFTTransferInfoRequest req) {

		NFTTransferListRes res = new NFTTransferListRes();

		try {
			String apiToken = config.getApiToken();
			String msg = config.conn().nftTransferInfo(apiToken, JSONUtil.toJsonStr(req));
			res = JSONUtil.toBean(msg, NFTTransferListRes.class);
		} catch (Exception e) {
			log.error("发生异常", e);
		}

		return res;
	}
	/**
	 * NFT生成
	 * userTradeCode 作为请求标识以及判断唯一参数
	 * owner 必须是上传到bsn上的链账户
	 * {"code":-1,"errorLogCode":"Field  invalid","errorLogMessage":"","message":"Parse error: Unrecognized token","data":null,"portalToken":null}
	 */
	public NFTMintRes nftMint(NFTMintRequest req) {
		NFTMintRes res = new NFTMintRes();
		try {
			String apiToken = config.getApiToken();
			String msg = config.conn().nftMint(apiToken, JSONUtil.toJsonStr(req));
			res = JSONUtil.toBean(msg, NFTMintRes.class);
		} catch (Exception e) {
			log.error("发生异常", e);
		}

		return res;
	}
	/**
	 * NFT发送
	 */
	public NFTSendRes nftSend(NFTSendRequest req) {

		NFTSendRes res = new NFTSendRes();
		try {
			String apiToken = config.getApiToken();
			String msg = config.conn().nftSend(apiToken, JSONUtil.toJsonStr(req));
			res = JSONUtil.toBean(msg, NFTSendRes.class);
		} catch (Exception e) {
			log.error("发生异常", e);
		}

		return res;
	}
	/**
	 * NFT销毁
	 */
	public NFTBurnRes nftBurn(NFTBurnRequest req) {

		NFTBurnRes res = new NFTBurnRes();
		try {
			String apiToken = config.getApiToken();
			String msg = config.conn().nftBurn(apiToken, JSONUtil.toJsonStr(req));
			res = JSONUtil.toBean(msg, NFTBurnRes.class);
		} catch (Exception e) {
			log.error("发生异常", e);
		}

		return res;
	}
	
}
